(ns appli.hadesWindow 

(:gen-class)
(:use read.readDB)
(:use appli.server)
(:use clojure.contrib.import-static)
)

(import '(javax.swing JFrame JButton JLabel JTextField JComponent JComboBox JPanel JTable ImageIcon JScrollPane JOptionPane)
        '(java.awt FlowLayout Color GridLayout Dimension  BorderLayout ComponentOrientation Component)
        '(javax.swing.table AbstractTableModel DefaultTableModel JTableHeader TableColumn DefaultTableCellRenderer TableCellEditor)
		'(java.awt.event ActionListener MouseListener MouseEvent KeyEvent KeyAdapter)
		'(java.awt Frame)
		'(java.io DataInputStream DataOutputStream OutputStream InputStream InputStreamReader BufferedReader PrintWriter)
		'(java.util Vector Arrays)
		'(java.net Socket ServerSocket))
			
(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_UP VK_DOWN VK_ENTER)		    

	
(defn convertToMap
	"Converts a list into a Map, to be able to pass it as parameter in the fill-JTable method."
	[list]
	{:records (vec list)})

(defn convertToVec
	"Converts a list into a Vec, to be able to pass it as parameter in the fill-JTable method."
	[list]
	(vec list))
			
(defn get-records-map 
	"Gets all the records from Data Base file, returns a map with the records."
	[lis]
	 ( convertToMap lis))
	
(defn get-records-vector
	"Gets all the records from Data Base file, returns a vector with the records."
	[file]
	(vec(for [n (file :records)] n)))

(defn count-records 
	"Counts the number of total records from the vector received as parameter."
	[vect]
	(count vect))
	
(defn filter-records-search 
	"Filters the records by location with three parameters: the search word, the DB file and which kind of search word it is(name or location). Returns a vector"
	[search-word file-records word-combox ] 
	(filter #(and 
	 		(= (:deleted %) false) ; primero veo si no esta borrado
	 		(= (.toLowerCase (str ((keyword word-combox) %))) (.toLowerCase (str search-word))) ; luego lo busco las concidencias con la word-combox
	 		) 
			file-records
	)
)

(defn filter-records
	"Filters the records again, checking if records are not erased.
	Receives a vector with the records as parameter." 
	[file-records] 
	(filter #(= (:deleted %) false) ; primero veo si no esta borrado	 		
			file-records
	)
)

(defn get-record-table 
 "Get selected row values and returns as a map " 
  [numOfRow tbl]
     (if (= (> numOfRow -1) true)
       {:name     (.getValueAt tbl numOfRow 0)
        :location (.getValueAt tbl numOfRow 1)  
        :size     (.getValueAt tbl numOfRow 2)
        :smoking  (.getValueAt tbl numOfRow 3)
        :rate     (.getValueAt tbl numOfRow 4)
        :date     (.getValueAt tbl numOfRow 5)
        :owner    (.getValueAt tbl numOfRow 6)
        :deleted false})
)

(defn get-position-in-Db 
 "Obtain the real position in the database of the record"
  [numOfRows file-records record pos]
  (loop [i 0]
		(when (< i numOfRows)
			(if (= (= (file-records i) record) true) (reset! pos i))
			(recur (inc i)))) 
)	

(defn fill-JTable
	"Fills the JTable with the map sent as parameter transformed from the vector of those records. 
	 Receives two parameters: map of the records to fill the table and the lecture of the Data Base File."
	[dbRecords read-Db]
	(proxy [AbstractTableModel] [] 
		  	(getRowCount [] (count (get dbRecords :records))) 
			  (getColumnCount [] (get read-Db :num-fields))
			  (isCellEditable  [row col] true)
  			(getColumnName [col]                            
				   (.toUpperCase (str (first (nth (get read-Db :fields) col)))))
   			(getValueAt [row col] (get (nth (get dbRecords :records) row) 
				    (keyword (str (first (nth (get read-Db :fields) col))))))
	)
)

(defn fill-JTable-DefaultModel
	"Fills the JTable with the database elements.
	 Receives five parameters: table defined, the model of the table, row position, column position, value"
	[table defModel get-row get-column get-value]
	
	(dorun (for [i (range 0 (.getColumnCount table))]
	(
		.addColumn defModel (.getColumnName table i)
	 ))) ;end dorun columns
	
	(dorun (for [j (range 0 (.getRowCount table))]
	(
		.addRow defModel (to-array [(.getValueAt table j 0) (.getValueAt table j 1) (.getValueAt table j 2) (.getValueAt table j 3) (.getValueAt table j 4) (.getValueAt table j 5) (.getValueAt table j 6)])
	 ))) ;end dorun rows
	
	  (.setModel table defModel)
	 
)


(defn change-Foreground-table 
 "Change the Jtable Foreground Color"   
    [t]
 
	(.setDefaultRenderer t Object (proxy [DefaultTableCellRenderer] []
	    (getTableCellRendererComponent [tbl obj isSelected hasFocus row cols]
	      (proxy-super getTableCellRendererComponent tbl obj isSelected hasFocus row cols)
	      
 	       (doto this
 	          (.setForeground  (condp =    (or (= (.getValueAt tbl   row 6 ) "") (= (.getValueAt tbl   row 6 ) " "))                                   
                            true (new Color 0x0000FF) ; row without owner 
                            false ( new Color  0xFF1493); row with owner 
                            Color/white))       
          )
	   )
	  )
	 )
) 

;****************************************************** SearchResult ***********************************************************************
(defn windowSearchResult
   "Displays the new Window with the result of a search. 
    Receives three parameters: title name of the window, the data base lecture,  the map of what the user is looking for. "
  [title  read-Db searchMap ]
  (let [frame     	(JFrame. title)
        panelRight 	(JPanel. )
        flowlayout 	(new FlowLayout )
        gridLayout 	(GridLayout. 10 0 5 5)
        btnClose	(JButton. "Close")
		    btnDelete	(JButton. "Delete")
	    	frameImage 	(Frame.)
	    	model  		(fill-JTable searchMap read-Db)
		    table (proxy [JTable] [model]
		            (isCellEditable  [row col] false)
		          )
        scrollTable (JScrollPane. table)
        
        
        get-column-change        (atom -1)
        get-row-change           (atom -1)
        get-value-at-change      (atom " ") 
		
		handler-jframe  (proxy [ActionListener][]
			(actionPerformed [event]
				(.setVisible frame true)
				(.dispose frame)
			)
		)
		defaultModel (DefaultTableModel.)
   ]
	
	;------------------ Method to fill manually the table ----------------
		(fill-JTable-DefaultModel table defaultModel get-row-change get-column-change get-value-at-change)
	;---------------------------------------------------------------------
        
	(.setDefaultCloseOperation frame JFrame/HIDE_ON_CLOSE)
	(.setIconImage frame (.getImage (ImageIcon. "IconoHades.png")))
    (.setAlignment flowlayout FlowLayout/TRAILING)
	(.setPreferredSize btnClose (Dimension. 28 75))
    (.setPreferredSize panelRight (Dimension. 130 20))
    (.setLayout panelRight gridLayout)
    (.setBackground panelRight (Color. 135 206 235))
	
	  
	(.addActionListener btnClose handler-jframe) 
 	(.add panelRight btnClose)
    (.add frame panelRight  BorderLayout/EAST)
  	(.add frame scrollTable)
	(.addMouseListener table 
		(proxy [MouseListener] [] 
			(mouseClicked [event] 
			(JOptionPane/showMessageDialog frame "Please make your changes on the main window.")) 
			(mouseEntered [event]) 
			(mouseExited  [event]) 
			(mousePressed  [event]) 
		    (mouseReleased [event])
		     ) 
		)
	(change-Foreground-table table)
    (.pack frame)
    (.setSize frame (Dimension. 800 270))
    (.setVisible frame true))
)

;*************************************************************Principal Window *************************************************************
(defn window
  "Displays a simple GUI window using the Swing API. The main Window of Hades Booking System Project. 
   Receives one paremeter: title name of the application.
   This is where the Interface is painted and manipulated so the main actions take place, adding handlers and listeners. "
  [title]
  (let [frame     		(JFrame. title)
        db        		(atom "db-1x2.db" ) 
 	    read-Db   		(read-bin-file @db )
        label     		(JLabel. "Search: " )
        imagelogo 		(ImageIcon. "Images/logohade.png")
        imagelegend 	(ImageIcon. "Images/leyenda.png")
        imagelegend2	(ImageIcon. "Images/leyenda2.png")
        logo        	(JLabel. imagelogo) 
        legend     		(JLabel. imagelegend) 
        legend2     	(JLabel. imagelegend2) 
        txtSearch 		(JTextField. 30 )
        combobox  		(JComboBox. )
        panelTop  		(JPanel. )
        panelRight 		(JPanel. )
        panelBottom 	(JPanel. )
        flowlayout 		(new FlowLayout )
        gridLayout 		(GridLayout. 10 0 5 5)
        btnFind			(JButton. "Find")
	    btnAdd 			(JButton. "Add")
		btnDelete		(JButton. "Delete")
		btnRefresh 		(JButton. "Refresh") 
		get-column-change        (atom -1)
        get-row-change           (atom -1)
        get-value-at-change      (atom " ")
		get-pos                  (atom -1) 
		get-record-value         (atom " ") 
		recordsVector     (get-records-vector read-Db)
        recordsMap       (get-records-map (filter-records recordsVector) )    
        model  	(fill-JTable recordsMap read-Db)
	    table (JTable. model)
	    defaultModel (DefaultTableModel.)
	    table2 (JTable. defaultModel) 
        scrollTable (JScrollPane. table2)
	    number-records ( count-records recordsVector)
	      
	       
  ;------------Method search---------------------------------------------------------------------
		handlerBtnFind  (proxy [ActionListener][]
			(actionPerformed [event]
	        
				(if (= {:records []} (convertToMap (filter-records-search(.getText txtSearch) recordsVector (.getSelectedItem combobox)))) (JOptionPane/showMessageDialog frame (str "-" (.getText txtSearch) "-" " not found searching by " (.getSelectedItem combobox)) "No results found" JOptionPane/INFORMATION_MESSAGE)
				(windowSearchResult "Search Result" read-Db  (convertToMap (filter-records-search(.getText txtSearch) recordsVector (.getSelectedItem combobox))) )
				)
			)
		)
	;--------------Handler button Add row-------------------------------------------------------				
		handlerBtnAdd  (proxy [ActionListener][]
			(actionPerformed [event]
				(.addRow defaultModel (to-array []))
				(write-empty-row "db-1x2.db" (count-records (get-records-vector (read-bin-file "db-1x2.db"))))
				
				 (.setVisible frame true) 						
				 (.dispose frame) 				  
				 (window "Hades Booking System")
			)
		)
	;--------------Handler button Deleted--------------------------------------------------------			
handlerBtnDelete (proxy [ActionListener][]
  (actionPerformed [event]
		(let [initialNumber  (.getSelectedRow table2)
				  record	       (get-record-table (.getSelectedRow table2) table2)  
				  pos            (atom -1) 
					]
			     (get-position-in-Db (count-records recordsVector)  recordsVector record pos)
					      (if (= (> @pos -1) true) 		(erase-record "db-1x2.db" @pos) 		) 
			       
					  (dorun (for [i (range (.getSelectedRow table2)(+ (.getSelectedRow table2) (count (.getSelectedRows table2))))]
						          (.removeRow defaultModel initialNumber)
					   ))
					   
							 		 (.setVisible frame true) 						
										(.dispose frame) 				  
										(window "Hades Booking System")		
				   
			)	
	)
)

handlerRefresh( proxy [ActionListener] []  
 (actionPerformed [event]
    (.setVisible frame true) 						
		(.dispose frame) 				  
		(window "Hades Booking System")
 )
)				
]
	
	;------------------ Method to fill manually the table ----------------
 	(fill-JTable-DefaultModel table defaultModel get-row-change get-column-change get-value-at-change)
	;---------------------------------------------------------------------
	
	;************************ Create the interface Java***********************************

	
   ;-----------------------PanelTop-------------------------------------
	  (.setIconImage frame (.getImage (ImageIcon. "Images/IconoHades.png")))
    (.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
    (.setBackground panelTop (Color. 135 206 235))
    (.setLayout panelTop flowlayout)
    (.setAlignment flowlayout FlowLayout/TRAILING)
    (.add panelTop combobox)
    (.add panelTop txtSearch)
    (.add panelTop label)
    (.addItem combobox "name")
    (.addItem combobox  "location")
    (.add panelTop logo)
    (.setComponentOrientation panelTop  ComponentOrientation/RIGHT_TO_LEFT)
    (.add frame panelTop  BorderLayout/NORTH )
    
   ;----------------AddActionListener to the buttons in the frame-----------------------------------
    (.addActionListener btnFind handlerBtnFind)
	(.addActionListener btnAdd handlerBtnAdd)
	(.addActionListener btnDelete handlerBtnDelete)
	(.addActionListener btnRefresh handlerRefresh) 
    (.setPreferredSize panelRight (Dimension. 160 60))
    (.setLayout panelRight gridLayout)
    (.setBackground panelRight (Color. 135 206 235))
         
    (.add panelRight btnFind )
    (.add panelRight btnAdd)
    (.add panelRight btnDelete)
    (.add panelRight btnAdd)
    (.add panelRight btnRefresh)
    (.add panelRight legend)
    (.add panelRight legend2) 
    (.add frame panelRight  BorderLayout/EAST)
     
		
    (.add frame scrollTable)
 
  ;--------------Add Listeners table -------------------------------------
        
  (.addKeyListener table2 
    (proxy [KeyAdapter] []
    
		(keyReleased [event]
			(if (and (not= @get-row-change -1) (not= get-column-change -1) (= (.isEditing table2) true))
				(reset! get-record-value (get-record-table (.getSelectedRow table2) table2))
			)
			
			(if (and (not= @get-row-change -1) (not= @get-column-change -1))  					 
			   (reset! get-value-at-change  (.getValueAt table @get-row-change @get-column-change))
			)         																			   
			
			(if (and (not= @get-value-at-change -1) (not=  @get-row-change -1) (=(.isEditing table2) false) 
			(or (= (.getKeyCode event) VK_ENTER) (= (.getKeyCode event) VK_LEFT) (= (.getKeyCode event) VK_RIGHT)
				(= (.getKeyCode event) VK_UP) (= (.getKeyCode event) VK_DOWN))) 
			
			(let [name     (str (.getValueAt table2 @get-row-change 0))
						location (str (.getValueAt table2 @get-row-change 1))						
						size     (str (.getValueAt table2 @get-row-change 2)) 							 							
						smoking  (str (.getValueAt table2 @get-row-change 3))					
						rate     (str (.getValueAt table2 @get-row-change 4))					
						date     (str (.getValueAt table2 @get-row-change 5))					
						owner    (str (.getValueAt table2 @get-row-change 6))				
						] 
						 (get-position-in-Db (count-records recordsVector)  recordsVector @get-record-value get-pos)
						  (if (> @get-pos -1) 		
							(println @get-pos)
							(write-row-with-values @db @get-pos name location size smoking rate date owner)
						)
						 
								(.repaint table2)
			 ) 
			  (reset! get-value-at-change -1) 
			) 					    
	)
		(keyTyped [event]
			(if (not= (.getEditingRow table2) -1)     (reset!   get-row-change (.getEditingRow table2))) 
			(if (not= (.getEditingColumn table2) -1)  (reset!   get-column-change (.getEditingColumn table2)) )					
		)
	) 
  )
	
	 (.addMouseListener table2 
	  (proxy [MouseListener] [] 
				(mouseClicked [event]) 
				(mouseEntered [event]) 
				(mouseExited  [event]) 
				(mousePressed  [event] 
				  
					(if (not= (.getEditingRow table2) -1)  (reset!   get-row-change (.getEditingRow table2))) 
			    (if (not= (.getEditingColumn table2) -1)  (reset!   get-column-change (.getEditingColumn table2)) )	
			    
			    (if (and (not= @get-row-change -1) (not= @get-column-change -1))  					 
					   (reset! get-record-value (get-record-table (.getSelectedRow table2) table2) ))
	    	) 
		    (mouseReleased [event])
		 ) 
	)
	
	 (change-Foreground-table table2) 
	 
	 ;--------Frame----------------------------------------------------------
	 
    (.pack frame)
    (.setSize frame (Dimension. 900 500))
    (.setVisible frame true))
    ;---------------------------------------------------------------------
    
    
)

;*************************************************************Principal WindowClient *************************************************************
(defn windowClient
  "Displays a simple GUI window using the Swing API. The main Window of Hades Booking System Project. 
   Receives one paremeter: title name of the application.
   This is where the Interface is painted and manipulated so the main actions take place, adding handlers and listeners. "
  [title socketClient input output serverAdd]
  (let [frame     		(JFrame. title)
        ;db        		(atom "db-1x2.db" )
		;db        		(atom (str (.getRemoteAddress socketClient) "appli\\db-1x2.db"))
		db        		(atom (str serverAdd "\\db-1x2.db"))
 	    read-Db   		(read-bin-file @db )
        label     		(JLabel. "Search: " )
        imagelogo 		(ImageIcon. "Images/logohade.png")
        imagelegend 	(ImageIcon. "Images/leyenda.png")
        imagelegend2	(ImageIcon. "Images/leyenda2.png")
        logo        	(JLabel. imagelogo) 
        legend     		(JLabel. imagelegend) 
        legend2     	(JLabel. imagelegend2) 
        txtSearch 		(JTextField. 30 )
        combobox  		(JComboBox. )
        panelTop  		(JPanel. )
        panelRight 		(JPanel. )
        panelBottom 	(JPanel. )
        flowlayout 		(new FlowLayout )
        gridLayout 		(GridLayout. 10 0 5 5)
        btnFind			(JButton. "Find")
	    btnAdd 			(JButton. "Add")
		btnDelete		(JButton. "Delete")
		btnRefresh 		(JButton. "Refresh")
		get-column-change        (atom -1)
        get-row-change           (atom -1)
        get-value-at-change      (atom " ")
		get-pos                  (atom -1) 
		get-record-value         (atom " ") 
		recordsVector     (get-records-vector read-Db)
        recordsMap       (get-records-map (filter-records recordsVector) )    
        model  	(fill-JTable recordsMap read-Db)
	    table (JTable. model)
	    defaultModel (DefaultTableModel.)
	    table2 (JTable. defaultModel) 
        scrollTable (JScrollPane. table2)
	    number-records ( count-records recordsVector)
	       
  ;------------Method search---------------------------------------------------------------------
		handlerBtnFind  (proxy [ActionListener][]
			(actionPerformed [event]
	        
				(if (= {:records []} (convertToMap (filter-records-search(.getText txtSearch) recordsVector (.getSelectedItem combobox)))) (JOptionPane/showMessageDialog frame (str "-" (.getText txtSearch) "-" " not found searching by " (.getSelectedItem combobox)) "No results found" JOptionPane/INFORMATION_MESSAGE)
				(windowSearchResult "Search Result" read-Db  (convertToMap (filter-records-search(.getText txtSearch) recordsVector (.getSelectedItem combobox))) )
				)
			)
		)
	;--------------Handler button Add row-------------------------------------------------------				
		handlerBtnAdd  (proxy [ActionListener][]
			(actionPerformed [event]
				(.addRow defaultModel (to-array []))
				(write-empty-row "db-1x2.db" (count-records (get-records-vector (read-bin-file "db-1x2.db"))))
				
				 (.setVisible frame true) 						
				 (.dispose frame) 				  
				 (windowClient "Hades Booking System" socketClient input output serverAdd)
			)
		)
	;--------------Handler button Deleted--------------------------------------------------------			
	handlerBtnDelete (proxy [ActionListener][]
		(actionPerformed [event]
			(let [initialNumber  (.getSelectedRow table2)
				  record	       (get-record-table (.getSelectedRow table2) table2)  
				  pos            (atom -1) 
					]
			     (get-position-in-Db (count-records recordsVector)  recordsVector record pos)
					      (if (= (> @pos -1) true) 		(erase-record "db-1x2.db" @pos) 		) 
			       
					  (dorun (for [i (range (.getSelectedRow table2)(+ (.getSelectedRow table2) (count (.getSelectedRows table2))))]
						          (.removeRow defaultModel initialNumber)
					   ))
					   
							 		 (.setVisible frame true) 						
										(.dispose frame) 				  
										(windowClient "Hades Booking System" socketClient input output serverAdd)
				   
			)	
		)
	)

	handlerRefresh( proxy [ActionListener] []  
		(actionPerformed [event]
			(.setVisible frame true) 						
			(.dispose frame) 				  
			(windowClient "Hades Booking System" socketClient input output serverAdd)
		)
	)		
]
	
	;------------------ Method to fill manually the table ----------------
 	(fill-JTable-DefaultModel table defaultModel get-row-change get-column-change get-value-at-change)
	;---------------------------------------------------------------------
	
	;************************ Create the interface Java***********************************

	
   ;-----------------------PanelTop-------------------------------------
	  (.setIconImage frame (.getImage (ImageIcon. "Images/IconoHades.png")))
    (.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
    (.setBackground panelTop (Color. 135 206 235))
    (.setLayout panelTop flowlayout)
    (.setAlignment flowlayout FlowLayout/TRAILING)
    (.add panelTop combobox)
    (.add panelTop txtSearch)
    (.add panelTop label)
    (.addItem combobox "name")
    (.addItem combobox  "location")
    (.add panelTop logo)
    (.setComponentOrientation panelTop  ComponentOrientation/RIGHT_TO_LEFT)
    (.add frame panelTop  BorderLayout/NORTH )
    
   ;----------------AddActionListener to the buttons in the frame-----------------------------------
    (.addActionListener btnFind handlerBtnFind)
	(.addActionListener btnAdd handlerBtnAdd)
	(.addActionListener btnDelete handlerBtnDelete)
	(.addActionListener btnRefresh handlerRefresh) 
    (.setPreferredSize panelRight (Dimension. 160 60))
    (.setLayout panelRight gridLayout)
    (.setBackground panelRight (Color. 135 206 235))
         
    (.add panelRight btnFind )
    (.add panelRight btnAdd)
    (.add panelRight btnDelete)
    (.add panelRight btnAdd)
    (.add panelRight btnRefresh)
    (.add panelRight legend)
    (.add panelRight legend2) 
    (.add frame panelRight  BorderLayout/EAST)
     
		
    (.add frame scrollTable)
 
  ;--------------Add Listeners table -------------------------------------
        
  (.addKeyListener table2 
    (proxy [KeyAdapter] []
    
				(keyReleased [event]
					
		     (if (and (not= @get-row-change -1) (not= get-column-change -1) (= (.isEditing table2) true))
			            (reset! get-record-value (get-record-table (.getSelectedRow table2) table2)))
			
			(if (and (not= @get-row-change -1) (not= @get-column-change -1))
			   (reset! get-value-at-change  (.getValueAt table @get-row-change @get-column-change))
			)
         																			   
					(if (and (not= @get-value-at-change -1) (not=  @get-row-change -1) (=(.isEditing table2) false)
					(or (= (.getKeyCode event) VK_ENTER) (= (.getKeyCode event) VK_LEFT) (= (.getKeyCode event) VK_RIGHT)
					    (= (.getKeyCode event) VK_UP) (= (.getKeyCode event) VK_DOWN)))
					
					(let [
							name	(str (.getValueAt table2 @get-row-change 0))
					 		location (str (.getValueAt table2 @get-row-change 1))
					 		size     (str (.getValueAt table2 @get-row-change 2))
					 		smoking  (str (.getValueAt table2 @get-row-change 3))
					 		rate     (str (.getValueAt table2 @get-row-change 4))
					 		date     (str (.getValueAt table2 @get-row-change 5))
					 		owner    (str (.getValueAt table2 @get-row-change 6))
							elementsToWrite		(str @get-pos "#" name "#" location "#" size "#" smoking "#" rate "#" date "#" owner)
							
						]
					
							(get-position-in-Db (count-records recordsVector) recordsVector @get-record-value get-pos)
							(if (> @get-pos -1)
								(if (not= owner "")
									(.writeUTF output (String. elementsToWrite))
									(.writeUTF output (str @get-pos "#" name "#" location "#" size "#" smoking "#" rate "#" date "#" " "))
								)
							)
							
							(.flush output)
							(.repaint table2)
					 )
					  (reset! get-value-at-change -1)
					) 					    
			)
			 (keyTyped [event]
		  		(if (not= (.getEditingRow table2) -1)     (reset!   get-row-change (.getEditingRow table2))) 
			    (if (not= (.getEditingColumn table2) -1)  (reset!   get-column-change (.getEditingColumn table2)) )					
		 	 )
		 ) 
	)
	
	 (.addMouseListener table2 
	  (proxy [MouseListener] [] 
				(mouseClicked [event]) 
				(mouseEntered [event]) 
				(mouseExited  [event]) 
				(mousePressed  [event] 
				  
				(if (not= (.getEditingRow table2) -1)  (reset!   get-row-change (.getEditingRow table2))) 
			    (if (not= (.getEditingColumn table2) -1)  (reset!   get-column-change (.getEditingColumn table2)) )	
			    
			    (if (and (not= @get-row-change -1) (not= @get-column-change -1))  					 
					   (reset! get-record-value (get-record-table (.getSelectedRow table2) table2) ))
	    	) 
		    (mouseReleased [event])
		 ) 
	)
	
	 (change-Foreground-table table2)
	 
	 ;--------Frame----------------------------------------------------------
	 
    (.pack frame)
    (.setSize frame (Dimension. 900 500))
    (.setVisible frame true))
    ;---------------------------------------------------------------------
    
    
)


(defn -main
	"This Method is responsable of sockets network conection between client and server, depending on the number of arguments."
	[& args]
		(if (= (first args) "alone")
			(window "Hades Booking System")
			(if (= (first args) "server")
				(serverLoop)
				(if (= (first args) "client")
					(let [
						socketClient (Socket. (last args) *port*)
						output			(DataOutputStream. (.getOutputStream socketClient))
						input			(DataInputStream. (.getInputStream socketClient))
						serverAdd		(.readUTF input)
						]
						(println (str "Server Address: " serverAdd))
						(windowClient "Hades Booking System Client" socketClient input output serverAdd)
					)
					(println "Select: alone, server, client ")
				)
			)
		)
)

;(window "Hades Booking System")